<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <PositionModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="system-position" setup>
  import { ref } from "vue"
  import { Icon } from "/@/components/Icon"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useModal } from "/@/components/Modal"
  import * as apis from "/@/api/iam/post"
  import { columns, searchFormSchema } from "./data"
  import PositionModal from "./form.vue"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { AvailableEnum } from '/@/enums'

  const { createMessage } = useMessage()
  const [registerModal, { openModal }] = useModal()
  const { prefixCls, tableContext } = useListPage({
    designScope: "position",
    tableProps: {
      title: "岗位列表",
      api: apis.pagePost,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 150,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    }
  })

  const [registerTable, { reload }] = tableContext

  function getActions(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "删除",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
       {
        label: "停用",
        color: "error",
        ifShow: record.status == AvailableEnum.NORMAL,
        popConfirm: {
          title: "确定停用吗?",
          confirm: handleUpdateStatus.bind(null, record, AvailableEnum.DISABLE),
        },
      },
      {
        label: "启用",
        ifShow: record.status == AvailableEnum.DISABLE,
        popConfirm: {
          title: "确定启用吗?",
          confirm: handleUpdateStatus.bind(null, record, AvailableEnum.NORMAL),
        },
      }
    ]
  }

  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    })
  }

  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  }

  async function handleDelete(record) {
    await apis.deletePost({ id: record.id }, reload)
  }

  
  const handleUpdateStatus = async (record, status) => {
    await apis.saveOrUpdatePost({ id: record.id, status }, true)
    createMessage.success("更新成功")
    await reload()
  }

</script>
